package com.huiminpay.merchant.service.impl;

import com.aliyun.oss.OSS;
import com.huiminpay.merchant.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class FileServiceImpl implements FileService {

    @Autowired
    OSS oss;

    @Value("${oss.aliyun.bucket}")
    private String bucket;
    @Value("${oss.aliyun.domain}")
    private String domain;


    /**
     * 文件上传
     *
     * @param file 文件对象
     * @return 文件地址
     */
    @Override
    public String uploadFile(MultipartFile file) {
        //获取原始文件名
        String originalFilename = file.getOriginalFilename();
        String filename = null;
        //截取扩展名
        if (originalFilename == null || !originalFilename.equals("")) {
            String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
            //生成新的文件名
            filename = System.currentTimeMillis() + ext;
            // 上传文件
            try {
                //属性存储空间名,文件名,文件流
                oss.putObject(bucket,filename,file.getInputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return domain+filename;
    }
}
